package Dao;

/*
时间：2021年5月10日10:30:08
作用：用于操作j_user表

 */

import java.sql.*;

import static util.DBUtil.closeAll;

public class UserDao {

    private static Connection connection = null;
    private static PreparedStatement preparedStatement = null;
    private static ResultSet resultSet = null;


    public UserDao() {
        try {
            Class.forName("oracle.jdbc.OracleDriver");
            connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "scott", "tiger");

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }


    //改

    public int update(String username1, String password1, String username2, String password2) {

        int reslut = 0;
        String sql = "update J_user set username =?," +
                "password = ? " + "where username =? and password =?";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username2);
            preparedStatement.setString(2, password2);
            preparedStatement.setString(3, username1);
            preparedStatement.setString(4, password1);
            reslut = preparedStatement.executeUpdate();


        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return -1;
        } finally {
            closeAll();
        }

        return reslut;
    }

    //删除

    public void delete(int id) {
        String sql = "delete from J_user where id =?";
        try {

            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            preparedStatement.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }

    }

    //查询

    public void query() {
        String sql = "select * from J_user";
        try {
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String username = resultSet.getString("username");
                String password = resultSet.getString("password");
                System.out.println(id + " " + username + " " + password);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }

    }

    //新增用户
    public int insert(String username, String password) {
        try {

            String sql = "insert into J_USER (id,username,password) values (seq_j_user.nextval,?,?)";
            preparedStatement = connection.prepareStatement(sql);


            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
            int rs = preparedStatement.executeUpdate();
            return rs;


        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        } finally {
            closeAll();
        }

    }


}

